<?php

namespace App\Jobs\System;

use App\Common\Traits\LogTrait;
use App\Models\System\SystemHourStatistics;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class SystemHourStatisticsJob implements ShouldQueue
{
    use Queueable, LogTrait;

    /**
     * Create a new job instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        try {
            //
            $hour= date('YmdH',strtotime('-1 hour'));
            $hourCount=SystemHourStatistics::where('statistic_name','system_views_hour')
                ->where('date_hour',(int)$hour)
                ->count();
            if($hourCount==0){
                SystemHourStatistics::create([
                    'statistic_name'=>'system_views_hour',
                    'date_hour'=>(int)$hour,
                    'statistic'=>cache('system_views_hour_'.$hour)??0,
                ]);
            }
        }catch (\Exception $e){
            $this->saveExceptionLog('记录系统访问量统计失败',$e);
        }

    }


}
